{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import broadlink,binascii\n",
    "def test1():\n",
    "    devices = broadlink.discover(timeout=5)\n",
    "    dir(devices[0])\n",
    "    devices[0].host\n",
    "    devices[0].mac\n",
    "    sw = broadlink.sp2(devices[0].host,devices[0].mac,None)\n",
    "    sw.auth()\n",
    "    sw.set_power(1)\n",
    "def test2():\n",
    "    mac_addr = binascii.unhexlify(\"78:0f:77:c8:b4:c0\".encode().replace(b':', b''))\n",
    "    sw2 = broadlink.sp2(devices[0].host,mac_addr,None)\n",
    "    sw2.auth()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "import yaml\n",
    "import random\n",
    "from aip import AipSpeech\n",
    "root_dir = \"/xiaobai/\"\n",
    "sys.path.append(root_dir)\n",
    "from xiaobai import XiaoBai,BaseSkill"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已执行，开灯\n",
      "已执行，关灯\n"
     ]
    }
   ],
   "source": [
    "import broadlink,binascii\n",
    "class SwitchSkill(BaseSkill):\n",
    "    def __init__(self):\n",
    "        #mac_addr = binascii.unhexlify(\"78:0f:77:c8:b4:c0\".encode().replace(b':', b''))\n",
    "        #print(mac_addr)\n",
    "        #self.sw = broadlink.sp2(('192.168.50.213',80),mac_addr,None)\n",
    "        devices = broadlink.discover(timeout=5)\n",
    "        self.sw = broadlink.sp2(devices[0].host,devices[0].mac,None)\n",
    "        if self.sw.auth()!=True:\n",
    "            print('认证失败，请重试')\n",
    "    #继承BaseSkill类，必须定义has_intent和action方法\n",
    "    def has_intent(self,text=\"\"):\n",
    "        for keyword in [\"打开\",\"关闭\",\"开灯\",\"关灯\"]:\n",
    "            if text.find(keyword)>= 0:\n",
    "                return True\n",
    "        return False\n",
    "    def action(self,text=\"\",callback=print):\n",
    "        try:\n",
    "            for keyword in [\"打开\",\"开灯\"]:\n",
    "                if text.find(keyword)>= 0:\n",
    "                    self.sw.set_power(1)\n",
    "                    callback(\"已执行，\"+text)\n",
    "                    return\n",
    "            for keyword in [\"关闭\",\"关灯\"]:\n",
    "                if text.find(keyword)>= 0:\n",
    "                    self.sw.set_power(0)      \n",
    "                    callback(\"已执行，\"+text)\n",
    "                    return\n",
    "        except:\n",
    "            callback(\"出错了呢，可能网络不太好\")\n",
    "if __name__ == '__main__':\n",
    "    import time\n",
    "    s = SwitchSkill()\n",
    "    s.handle(\"开灯\")\n",
    "    time.sleep(1)\n",
    "    s.handle(\"关灯\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Listening...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:snowboy:Keyword 1 detected at time: 2019-05-14 04:30:23\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你：打开开关\n",
      "小白：已执行，打开开关\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:snowboy:Keyword 1 detected at time: 2019-05-14 04:30:56\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你：关闭开关\n",
      "小白：已执行，关闭开关\n"
     ]
    }
   ],
   "source": [
    "keyword_model = root_dir+'resources/小白.pmdl'\n",
    "xiaobai = XiaoBai(keyword_model=keyword_model)\n",
    "xiaobai.add_skill(SwitchSkill())\n",
    "xiaobai.listen_for_keyword()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
